package com.jt.service;

import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class HttpClientUserServiceImpl implements HttpClientService{

    //jt-web 需要访问jt-sso获取数据  HttpClient
    //http://sso.jt.com/userfindUserList
    @Override
    public List<User> findUserList() {
        List<User> userList = new ArrayList<>();
        String url = "http://sso.jt.com/user/findUserList";
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if(httpResponse.getStatusLine().getStatusCode() == 200){
                String json =
                        EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
                userList = ObjectMapperUtil.toObject(json, userList.getClass());
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        return userList;
    }
}